<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title>帖子管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .search-box {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .search-box input, .search-box select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .search-box input {
            width: 200px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
        }
        .btn-primary {
            background-color: #1ab394;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-danger {
            background-color: #ed5565;
        }
        .btn-danger:hover {
            background-color: #ec4758;
        }
        .btn-info {
            background-color: #23c6c8;
        }
        .btn-info:hover {
            background-color: #21b9bb;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
            color: #1ab394;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
        .post-type {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            color: white;
        }
        .type-1 { background-color: #1ab394; }
        .type-2 { background-color: #f8ac59; }
        .type-3 { background-color: #23c6c8; }
    </style>
</head>
<body>
<div class="container">
    <div class="top-bar">
        <div class="search-box">
            <input type="text" id="searchTitle" placeholder="输入标题搜索" value="${param.title}">
            <select id="searchType">
                <option value="">全部类型</option>
                <option value="1" ${param.type == '1' ? 'selected' : ''}>学生帖子</option>
                <option value="2" ${param.type == '2' ? 'selected' : ''}>心理咨询</option>
                <option value="3" ${param.type == '3' ? 'selected' : ''}>就业指导</option>
            </select>
            <button class="btn btn-primary" onclick="search()">
                <i class="fas fa-search"></i> 搜索
            </button>
        </div>
    </div>

    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>类型</th>
            <th>发帖人</th>
            <th>浏览次数</th>
            <th>发帖时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${postList}" var="post">
            <tr>
                <td>${post.postId}</td>
                <td>${post.title}</td>
                <td>
                            <span class="post-type type-${post.postType}">
                                <c:choose>
                                    <c:when test="${post.postType == 1}">学生帖子</c:when>
                                    <c:when test="${post.postType == 2}">心理咨询</c:when>
                                    <c:when test="${post.postType == 3}">就业指导</c:when>
                                </c:choose>
                            </span>
                </td>
                <td>${post.username}</td>
                <td>${post.viewCount}</td>
                <td><fmt:formatDate value="${post.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td>
                    <button class="btn btn-info btn-sm" onclick="viewPost(${post.postId})">
                        <i class="fas fa-eye"></i> 查看
                    </button>
                    <button class="btn btn-danger btn-sm" onclick="deletePost(${post.postId})">
                        <i class="fas fa-trash"></i> 删除
                    </button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

    <div class="pagination">
        <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
            <i class="fas fa-chevron-left"></i> 上一页
        </button>

        <c:forEach begin="1" end="${totalPages}" var="i">
            <c:choose>
                <c:when test="${i == currentPage}">
                    <button class="active">${i}</button>
                </c:when>
                <c:otherwise>
                    <button onclick="goToPage(${i})">${i}</button>
                </c:otherwise>
            </c:choose>
        </c:forEach>

        <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
            下一页 <i class="fas fa-chevron-right"></i>
        </button>

        <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
    </div>
</div>

<script>
    function search() {
        const title = document.getElementById('searchTitle').value.trim();
        const type = document.getElementById('searchType').value;
        const params = new URLSearchParams();
        if (title) params.append('title', title);
        if (type) params.append('type', type);
        location.href = '${pageContext.request.contextPath}/admin/post/list?' + params.toString();
    }

    document.getElementById('searchTitle').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            search();
        }
    });

    function viewPost(id) {
        location.href = '${pageContext.request.contextPath}/admin/post/view?id=' + id;
    }

    function deletePost(id) {
        if (confirm('确定要删除这个帖子吗？')) {
            fetch('${pageContext.request.contextPath}/admin/post/delete?id=' + id, {
                method: 'POST'
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert(data.message || '删除失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
        }
    }

    function goToPage(page) {
        const title = document.getElementById('searchTitle').value.trim();
        const type = document.getElementById('searchType').value;
        const params = new URLSearchParams();
        params.append('page', page);
        if (title) params.append('title', title);
        if (type) params.append('type', type);
        location.href = '${pageContext.request.contextPath}/admin/post/list?' + params.toString();
    }
</script>
</body>
</html> 